<?php

/**
 * 卖家中心通用控制器
 *
 * @author birdy <freebirdy@foxmail.com>
 * @link http://www.aiyuangong.com
 */

namespace app\common\controller;

class Seller extends Front
{
    protected $map_default = [];

    //强制登录
    protected $check_login = true;
    
    //店家账号ID
    protected $seller_id = 0;
    
    //消息读取权限
    protected $seller_smt_limits = '*';

    //初始化
    protected function _initialize()
    {
        parent::_initialize();
        if ($this->store_id < 1) {
            $this->error('你当前登录的账号，非供应商账号', 'index/store/join');
        }
        $this->seller_id = $this->store_info['seller_id'];
        config('seller_id', $this->seller_id);
        config('seller_smt_limits', $this->seller_smt_limits);
    }
    
    /**
     * 获取店铺绑定的分类
     */
    protected function getStoreClass()
    {
        $where = [];
        $where['store_id'] = $this->store_id;
        $where['state'] = 1;
        $goods_class = rpc("common/common/lists", ['store_bind_class', $where]);
        $rs = [];
        $mall_cate = \Fields::getById('goods_class'); // config('mall_cate');
        foreach ($goods_class as $goods_class) {
            $cls1 = $goods_class['class_1'];
            $cls2 = $goods_class['class_2'];
            if (!isset($rs[$cls1])) {
                $rs[$cls1] = [
                    'name' => $mall_cate[$cls1]['name'],
                    'tag' => $mall_cate[$cls1]['tag']
                ];
            }
            $rs[$cls1]['_child'][$goods_class['class_2']] = [
                'name' => $mall_cate[$cls1]['_child'][$cls2]['name'],
                'tag' => $mall_cate[$cls1]['_child'][$cls2]['tag']
            ];
        }
        return $rs;
    }

    /**
     * 记录卖家日志
     *
     * @param $content 日志内容
     * @param $state 1成功 0失败
     */
    protected function recordSellerLog($content = '', $state = 1){
        $seller_info = array();
        $seller_info['log_content'] = $content;
        $seller_info['log_time'] = TIMESTAMP;
        $seller_info['log_seller_id'] = $this->seller_id;
        $seller_info['log_seller_name'] = $this->store_info['seller_name'];
        $seller_info['log_store_id'] = $this->store_id;
        $seller_info['log_seller_ip'] = $this->request->ip();
        $seller_info['log_url'] = $this->request->controller().'&'.$this->request->action();
        $seller_info['log_state'] = $state;
        $model_seller_log = model('Common/SellerLog');
        return $model_seller_log->addSellerLog($seller_info);
    }
    
    /**
     * 添加到任务队列
     *
     * @param array $goods_array
     * @param boolean $ifdel 是否删除以原记录
     */
    protected function addcron($data = array(), $ifdel = false) {
        $model_cron = Model('cron');
        if (isset($data[0])) { // 批量插入
            $where = array();
            foreach ($data as $k => $v) {
                if (isset($v['content'])) {
                    $data[$k]['content'] = serialize($v['content']);
                }
                // 删除原纪录条件
                if ($ifdel) {
                    $where[] = '(type = ' . $data['type'] . ' and exeid = ' . $data['exeid'] . ')';
                }
            }
            // 删除原纪录
            if ($ifdel) {
                $model_cron->delCron(implode(',', $where));
            }
            $model_cron->addCronAll($data);
        } else { // 单条插入
            if (isset($data['content'])) {
                $data['content'] = serialize($data['content']);
            }
            // 删除原纪录
            if ($ifdel) {
                $model_cron->delCron(array('type' => $data['type'], 'exeid' => $data['exeid']));
            }
            $model_cron->addCron($data);
        }
    }
}
